package com.h2cloud.rolling;

import java.text.SimpleDateFormat;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author: wulongkai
 * @create: 2023-05-26 14:29
 **/
public class windows {

    public static void main(String[] args) {
        windows l = new windows();
        for (Map.Entry<Integer, String> monthsConfig : l.getMonthsConfigs().entrySet()) {
            System.out.println(monthsConfig.getKey() + " " + monthsConfig.getValue());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    }

    private Map<Integer, String> getMonthsConfigs() {
        int beginMonth = 202402;
        int endMonth = getEndMonth(beginMonth);

        int startYear = beginMonth / 100;
        int startMonth = beginMonth % 100;

        int endYear = endMonth / 100;
        int endMonthValue = endMonth % 100;

        Map<Integer, String> monthsMap = new TreeMap<>();
        DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM");

        YearMonth start = YearMonth.of(startYear, startMonth);
        YearMonth end = YearMonth.of(endYear, endMonthValue);
        YearMonth current = start;
        while (current.isBefore(end) || current.equals(end)) {
            int year = current.getYear();
            int month = current.getMonthValue();
            monthsMap.put(year * 100 + month, current.format(sdf));
            current = current.plusMonths(1);
        }

        return monthsMap;
    }

    private int getEndMonth(long beginMonth) {
        String begin = String.valueOf(beginMonth);
        String end;
        if (begin.substring(4).startsWith("0")) {
            end = begin.substring(0, 4) + "12";
        } else {
            end = addOne(begin.substring(0, 4)) + "12";
        }
        return Integer.parseInt(end);
    }

    private String addOne(String str) {
        int i = Integer.parseInt(str) + 1;
        return String.valueOf(i);
    }
}
